package org.csu.code.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.code.api.common.CommonResponse;
import org.csu.code.api.entity.Sentence;
import org.csu.code.api.persistence.SignonMapper;
import org.csu.code.api.persistence.TalkMapper;
import org.csu.code.api.service.TalkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import org.csu.code.api.service.RedisService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TalkServiceImpl implements TalkService {
    @Autowired
    private TalkMapper talkMapper;

    @Autowired
    private SignonMapper signonMapper;

    @Autowired
    private RedisService redisService;

//    @Override
//    public CommonResponse<List<Sentence>> getTalkList(String username) {
//        List<Sentence> talkList = talkMapper.getTalkList(username);
//        return CommonResponse.createForSuccess(talkList);
//    }
//
//    @Override
//    public CommonResponse<List<String>> getNameList() {
//        List<String> nameList = talkMapper.getNameList();
//        return CommonResponse.createForSuccess(nameList);
//    }

    @Override
    public CommonResponse<String> insertSentence(HttpServletRequest request,Sentence sentence) {
        try{

            String token = request.getHeader("token");
            int userid = Integer.parseInt((String) redisService.get(token));
            sentence.setUserid(userid);
            String name = signonMapper.selectById(sentence.getUserid()).getUsername();
            Date date =new Date();
            SimpleDateFormat  SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat.format(date.getTime());
            sentence.setDatetime(date);// new Date()为获取当前系统时间
            sentence.setUsername(name);
            talkMapper.insert(sentence);
            return CommonResponse.createForSuccess("发送成功!");
        }catch (Exception e)
        {
            e.printStackTrace();
            return CommonResponse.createForError("发送失败!");
        }
    }

    @Override
    public CommonResponse<String> getTotalTalk(HttpServletRequest request, int toUerId) {
        try {
            String token = request.getHeader("token");
            int userid = Integer.parseInt((String) redisService.get(token));
            QueryWrapper<Sentence> sentenceQueryWrapper = new QueryWrapper<>();
            sentenceQueryWrapper.and(wrapper -> wrapper.eq("userid", userid).eq("toUserId", toUerId));
            System.out.println("userid " + userid + " toUserId " + toUerId);
            List<Sentence> sentenceList = talkMapper.selectList(sentenceQueryWrapper);
            System.out.println(sentenceList);
            List<String> nameList = new ArrayList<String>();
            for (int i = 0; i < sentenceList.size(); i++) {
                nameList.add(sentenceList.get(i).getContent());
            }
            return CommonResponse.createForSuccess(nameList.get(sentenceList.size() - 1));
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResponse.createForError("发送失败!");
        }
    }

}
